package com.xuanwei.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuanwei.core.exception.XwException;
import com.xuanwei.core.service.IXwHelpContentService;
import com.xuanwei.core.service.IXwHelpService;
import com.xuanwei.dao.entity.XwHelp;
import com.xuanwei.dao.entity.XwHelpContent;
import com.xuanwei.dao.mapper.XwHelpContentMapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mjy
 * @since 2019-08-27
 */
@Service
public class XwHelpContentServiceImpl extends ServiceImpl<XwHelpContentMapper, XwHelpContent> implements IXwHelpContentService {

    private IXwHelpService helpService;

    public XwHelpContentServiceImpl(IXwHelpService helpService) {
        this.helpService = helpService;
    }

    @Override
    public void addOrUpdate(XwHelpContent content) {
        XwHelp byId = helpService.getById(content.getHelpId());
        if (byId == null) {
            throw new XwException("数据不存在");
        }

        if (content.getId() == null) {
            content.setCreateTime(LocalDateTime.now());
            content.setUpdateTime(LocalDateTime.now());
            save(content);
        } else {
            content.setUpdateTime(LocalDateTime.now());
            XwHelpContent helpContent = getById(content.getId());
            if (helpContent == null) {
                throw new XwException("数据不存在");
            }
            updateById(content);
        }

    }

    @Override
    public void updateStatus(Integer id) {
        XwHelpContent helpContent = getById(id);
        if (helpContent == null) {
            throw new XwException("数据不存在");
        }
        helpContent.setUpdateTime(LocalDateTime.now());
        helpContent.setShowStatus(helpContent.getShowStatus() == 0 ? 1 : 0);
        updateById(helpContent);
    }

    @Override
    public List<XwHelpContent> findAll(Integer helpId, Integer type) {
        QueryWrapper<XwHelpContent> wrapper = new QueryWrapper<XwHelpContent>()
                .eq("help_id", helpId)
                .orderByAsc("order_by")
                .orderByDesc("create_time");

        if (type != null && type == 0) {
            wrapper.eq("show_status", 0);
        }
        return list(wrapper);
    }
}
